package com.srini.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.srini.pojos.LoggedInUser;
import com.srini.pojos.UserInfo;
import com.srini.util.Constants;
import com.vssr.persist.DataBaseWriter;

public class UpdateUserProfile extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		System.out.println("Updating user profile....");
		// String[] wheeler = { "2-Wheeler", "4-Wheeler" };
		LoggedInUser user = (LoggedInUser) req.getSession().getAttribute(
				Constants.LOGGEDIN_USER);
		String firstName = req.getParameter("firstname");
		String lastName = req.getParameter("lastname");
		String distance = req.getParameter("distance");
		String vehicle = req.getParameter("vehicle_type");
		String address = req.getParameter("address");
		String empId = req.getParameter("empId");
		String manager = req.getParameter("manager");
		String project = req.getParameter("project");
		String workPlace = req.getParameter("workplace");

		UserInfo info = user.getUserInfo();
		info.setUserId(user.getId());
		info.setAddress(address);
		info.setVehicle(vehicle);
		info.setDistanceFromHome(distance);
		info.setFirstName(firstName);
		info.setLastName(lastName);
		info.setManager(manager);
		info.setProject(project);
		info.setEmpId(empId);
		info.setWorkPlace(workPlace);
		// user.setUserInfo(info);
		DataBaseWriter.update(info);
		req.setAttribute("message", "User profile successfully updated.");
		req.setAttribute("currentPage", "userprofile");
		req.getRequestDispatcher("/home.jsp").forward(req, resp);
	}
}
